package com.feijin.testwisdomzonedemo.actions;


import com.feijin.testwisdomzonedemo.net.RMer;
import com.lgc.garylianglib.actions.Action;
import com.lgc.garylianglib.actions.ActionCreator;
import com.lgc.garylianglib.event.EventBusUtils;
import com.lgc.garylianglib.event.StoreEvent;
import com.lgc.garylianglib.model.BaseDto;
import com.lgc.garylianglib.net.retrofit.DefResponseCallBackImpl;
import com.lgc.garylianglib.util.L;

import retrofit2.Call;
import retrofit2.Response;

/**
 * <pre>
 *     author : feijin_lgc
 *     e-mail : 595184932@qq.com
 *     time   : 2018/1/4 17:20
 *     desc   :    基类
 *     version: 1.0
 * </pre>
 */
public class BaseAction<V> extends ActionCreator {
    public V view;

    public void attachView(V view) {
        this.view = view;
    }

    public void dettachView() {
        this.view = null;
    }

    public BaseAction() {
    }

    /**
     * 取消访问
     */
    public void cancelNet() {
        if (baseDtoCall != null) {
            baseDtoCall.cancel();
        }

    }


    public Call<BaseDto> baseDtoCall;

    /**
     * 访问   基类
     *
     * @param call
     * @param successType
     * @return
     */
    public Call<BaseDto> post(RMer rMer, Call<BaseDto> call, final String successType, final boolean isHasResp) {
        rMer.enqueue(call, new DefResponseCallBackImpl<BaseDto>() {
            @Override
            public void onSuccess(Call<BaseDto> call, Response<BaseDto> response) {
                L.e("xx", "onSuccess." + successType + "  " + response.body().toString());
                if (isHasResp) {
                    L.e("MySharedPreferencesUtil", "成功登陆onSuccess." + successType + "  " + response.body().toString());

                    sendResp(successType, response);
                }
                sendEvent(successType, response.body().getResult(),
                        Action.KEY_OBJ, response.body());
            }

            @Override
            public void onFailure(Call<BaseDto> call, Response<BaseDto> response, int status) {
                if (response.body().getResult() == -2) {
                    if (noLoginListener != null) {
                        noLoginListener.tologin();
                    }
                }
                sendEvent(StoreEvent.ACTION_KEY_ERROR, response.body().getResult(),
                        Action.KEY_MSG, response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseDto> call, Throwable t) {
                sendEvent(StoreEvent.ACTION_KEY_ERROR, netError,
                        Action.KEY_MSG, NETWORK_TIPS);
            }
        });
        return call;
    }
    public Call<BaseDto> post(RMer rMer, Call<BaseDto> call, final String successType, final String errorType, final boolean isHasResp) {
        rMer.enqueue(call, new DefResponseCallBackImpl<BaseDto>() {
            @Override
            public void onSuccess(Call<BaseDto> call, Response<BaseDto> response) {
                L.e("xx", "onSuccess." + successType + "  " + response.body().toString());
                if (isHasResp) {
                    L.e("MySharedPreferencesUtil", "成功登陆onSuccess." + successType + "  " + response.body().toString());

                    sendResp(successType, response);
                }
                sendEvent(successType, response.body().getResult(),
                        Action.KEY_OBJ, response.body());
            }

            @Override
            public void onFailure(Call<BaseDto> call, Response<BaseDto> response, int status) {
                if (response.body().getResult() == -2) {
                    if (noLoginListener != null) {
                        noLoginListener.tologin();
                    }
                }
                sendEvent(errorType, response.body().getResult(),
                        Action.KEY_MSG, response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseDto> call, Throwable t) {
                sendEvent(errorType, netError,
                        Action.KEY_MSG, NETWORK_TIPS);
            }
        });
        return call;
    }
    /**
     * 增加数据的返回
     *
     * @param successType
     * @param response
     */
    public void sendResp(String successType, Response<BaseDto> response) {

    }

    public void register(Object subscriber) {

        mSubscriber = subscriber;
        EventBusUtils.register(subscriber);
    }

    public Object mSubscriber;

    public void unregister(Object subscriber) {
        mSubscriber = null;
        EventBusUtils.unregister(subscriber);
    }

    public void unregister() {
        EventBusUtils.unregister(mSubscriber);
    }

    public NoLoginListener getNoLoginListener() {
        return noLoginListener;
    }

    public void setNoLoginListener(NoLoginListener noLoginListener) {
        this.noLoginListener = noLoginListener;
    }

    public NoLoginListener noLoginListener;

    public interface NoLoginListener {
        void tologin();
    }
}
